package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.dto.OrdersPageDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-06-06
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    @PostMapping("/submit")
    public R<String> addOrders(@RequestBody Orders orders) {
        log.info("{}", orders);
        ordersService.saveByOrders(orders);
        return R.success("支付成功");
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> selectPage(int page, int pageSize) {
        Page<OrdersDto> ordersDtoPage = ordersService.selectByPage(page, pageSize);
        return R.success(ordersDtoPage);
    }

    @GetMapping("/page")
    public R selectPageBackstage(OrdersPageDto ordesrPageDto) {
        int pageSize = ordesrPageDto.getPageSize();
        int page = ordesrPageDto.getPage();
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //添加条件查询
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        //订单号的模糊查询,先判断是否为空
        qw.like(StringUtils.isNotEmpty(ordesrPageDto.getNumber()), Orders::getNumber, ordesrPageDto.getNumber());

        //设置开始时间，结束时间
        boolean time = ordesrPageDto.getBeginTime() != null && ordesrPageDto.getEndTime() != null;
        LocalDateTime beginTime = ordesrPageDto.getBeginTime();
        LocalDateTime endTime = ordesrPageDto.getEndTime();
        qw.between(time, Orders::getOrderTime, beginTime, endTime);
        //对时间进行排序 倒序
        qw.orderByDesc(Orders::getOrderTime);

        //通过page查询数据
        ordersService.page(pageInfo, qw);

        //查询出的数据进行修改
        List<Orders> records = pageInfo.getRecords();
        for (Orders record : records) {
            //设置用户名，如果是空就把收货人为name
            String userName = record.getUserName();
            if (userName == null) {
                record.setUserName(record.getConsignee());
            }
        }
        //重新赋值
        pageInfo.setRecords(records);
//        Page<OrdersDto> ordersDtoPage = ordersService.selectByPage(page, pageSize);
        return R.success(pageInfo);
    }

    @PutMapping
    public R updateByStatus(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

    @PostMapping("/again")
    public R again(@RequestBody Orders orders) {
        ordersService.again(orders);
        return R.success("支付成功");
    }
}

